package tree;

/**
 * @Author leecho
 * @Date: 2021/10/26 10:04
 * @Version 1.0
 */

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        threadedNode threadedNode = new threadedNode(0);
        threadedBinaryTree threadedBinaryTree = new threadedBinaryTree(threadedNode);
        tree.threadedNode node1 = new threadedNode(1);
        tree.threadedNode node2 = new threadedNode(2);
        tree.threadedNode node3 = new threadedNode(3);
        tree.threadedNode node4 = new threadedNode(4);
        tree.threadedNode node5 = new threadedNode(5);
        tree.threadedNode node6 = new threadedNode(6);

        threadedNode.setLeft(node1);
        threadedNode.setRight(node2);

        node1.setLeft(node3);
        node1.setRight(node4);

        node2.setLeft(node5);
        node2.setRight(node6);

        threadedBinaryTree.threaded(threadedNode);
        threadedNode right = node3.getRight();
        System.out.println(right.getNo());
        System.out.println("=========");
        threadedBinaryTree.threadedList();
    }
}

class threadedBinaryTree{
    private threadedNode head;
    private threadedNode pre;


    public threadedBinaryTree(threadedNode head) {
        this.head = head;
    }

    public void threaded(threadedNode node){
        if (node == null){
            return;
        }
        threaded(node.getLeft());
        if (node.getLeft() == null){
            node.setLeft(pre);
            node.setLeftType(1);
        }
        if (pre != null && pre.getRight() == null){
            pre.setRight(node);
            pre.setRightType(1);
        }
        pre = node;
        threaded(node.getRight());
    }

    public void threadedList(){
        threadedNode root =head;

        while (head != null){
            while (head.getLeftType() == 0){
                head = head.getLeft();
            }

            System.out.println(head.getNo());

            while (head.getRightType() == 1){
                head = head.getRight();
                System.out.println(head.getNo());
            }
            head = head.getRight();
        }
    }
}

class threadedNode{
    private int no;
    private threadedNode left;
    private threadedNode right;
    private int leftType;
    private int rightType;

    public threadedNode(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public threadedNode getLeft() {
        return left;
    }

    public void setLeft(threadedNode left) {
        this.left = left;
    }

    public threadedNode getRight() {
        return right;
    }

    public void setRight(threadedNode right) {
        this.right = right;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }
}